<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>中联众鑫建筑机械</title>
<link rel="stylesheet" media="all" type="text/css" href="admin.css" />
<script type="text/javascript" charset="utf-8" src="../ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="../ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="../ueditor/third-party/jquery-1.10.2.min.js"> </script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" charset="utf-8" src="../ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
	function saveNew() {
		if (!validate()) {
			return;
		}
		
		var content = UE.getEditor('editor').getContent();
		var contentInput = document.getElementById("content");
		var title = $("#title").val();
		var author = $("#author").val();
		var id = $("#id").val();
		contentInput.value = content;
		$.ajax({
			type : "POST",
			url : "new_action.php",
			data : {id : id, act : "save", title : title, author : author, content : content},
			success : function(data) {
				if ("success" == data) {
					alert("保存成功");
					location.href = "new_list.php";
				}
			},
			error : function(data) {
				alert(data);
			}
		});
	}

	function validate() {
		if (!addNewForm.title.value) {
			alert("请输入标题!");
			return false;
		}

		if (!addNewForm.author.value) {
			alert("请输入作者!");
			return false;
		}

		if (!UE.getEditor('editor').getContent()) {
			alert("请输入内容!");
			return false;
		}
		return true;
	}
</script>
</head>
<body>
<div class="center">
	<?php include 'top.php';
		include '../business/dao/BaseDataAccess.php';
		include '../business/dao/NewsAccess.php';
		ini_set('display_errors',1);

		$newsDao = new NewsDao();
		$title = "";
		$author = "";
		$content = "";
		$id = null;
		if (isset($_GET["id"])) {
			$new = $newsDao->getNew($_GET["id"]);
			$title = $new->getTitle();
			$author = $new->getAuthor();
			$content = $new->getContent();
			$id = $new->getId();
		}
	?>
	<div class="box centent">
		<div class="right">
			<div class="title">添加新闻</div>
			<div>
				<form name="addNewForm" method="post" action="add_new_action.php" class="autoform">
					<input id="id" name="id" type="hidden" value="<?php echo $id?>" />
					<table border="0" cellpadding="5" cellSpacing="5">
						<tr>
							<td width="50" class="label">标题:</td>
							<td class="field"><input id="title" name="title" type="text" style="width:500px;" value="<?php echo $title?>" /></td>
						</tr>
						<tr>
							<td class="label">作者:</td>
							<td class="field"><input id="author" name="author" type="text" style="width:100px;" value="<?php echo $author?>"/></td>
						</tr>
						<tr>
							<td colspan="2">
								<script id="editor" type="text/plain" style="height:500px;"></script>
								<input id="content" name="content" type="hidden" />
							</td>
						</tr>
						<tr>
							<td colspan="2" align="center"><input type="button" value="提 交" onclick="saveNew();">&nbsp;&nbsp;<input type="reset" value="重 置"></td>
						</tr>
					</table>
				</form>
			</div>
		</div>
	</div>
	<?php include 'bottom.php';?>
</div>
</body>
<script type="text/javascript">
    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    var editor = UE.getEditor('editor');
    editor.addListener("ready", function() {
        editor.setContent("<?php echo $content?>");
    });
</script>
</html>